package ch.fhnw.qualiscan.application.pages.tabs;

import java.util.List;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.modals.AdminAddUniversity;
import ch.fhnw.qualiscan.application.modals.AdminEditUniversity;
import ch.fhnw.qualiscan.application.pages.Admin;
import ch.fhnw.qualiscan.core.db.pojos.University;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

/**
 * Edit universities.
 */
@SuppressWarnings("serial")
public class AdminTab5 extends VerticalLayout{

	public Admin admin;
	private Table universityTable;
	public BeanItemContainer<University> tableContainer ;
	
	public AdminTab5(Admin admin) {
		this.admin = admin;
		
		buildLayout();
	}
	
	public void buildLayout() {
		removeAllComponents();
		
		addComponent(addUniversityForm());
		
		VerticalLayout tableWrap = new VerticalLayout();
		tableWrap.addComponent(buildTable());
		tableWrap.setMargin(true);
		addComponent(tableWrap);
	}
	
	public Table buildTable() {
		tableContainer = new BeanItemContainer<University>(University.class);
		
		List<University> universityList = admin.adminModel.getUniversities();
		for(int i = 0; i < universityList.size(); i++) {
			tableContainer.addBean(universityList.get(i));
		}
		
		universityTable = new Table();
		universityTable.setWidth("100%");
		universityTable.setPageLength(0);
		universityTable.setSelectable(true);
		
		
		universityTable.addGeneratedColumn("universityEditButton", new Table.ColumnGenerator() {
			public Component generateCell(Table source, Object itemId, Object columnId) {
			
 				Button editButton = new Button(TM.get("action.edit"), new Button.ClickListener() {
					public void buttonClick(ClickEvent event) {
						University university = (University) event.getButton().getData();
						AdminTab5 caller = (AdminTab5) event.getButton().getParent().getParent().getParent();
						admin.openModal(new AdminEditUniversity(university, caller));
					}
				});
 				editButton.setData(itemId);
				editButton.setIcon(Config.ICON_EDIT);
				return editButton;
			}
		});
		
		universityTable.setContainerDataSource(tableContainer);
		
		universityTable.setColumnHeader("title", TM.get("admin.universitytable.title"));
		universityTable.setColumnHeader("universityEditButton", TM.get("admin.edituniversity.title"));

		
		universityTable.setVisibleColumns(new Object[]{"title", "universityEditButton"});
		
		return universityTable;
	}
	
	public HorizontalLayout addUniversityForm() {
		HorizontalLayout addDomainForm = new HorizontalLayout();
		addDomainForm.setMargin(true, true, false, true);
		
		Button addButton =  new Button(TM.get("action.add"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				AdminTab5 caller = (AdminTab5) event.getButton().getParent().getParent();
				admin.openModal(new AdminAddUniversity(caller));
			}
		});
		addButton.setIcon(Config.ICON_ADD);
		addDomainForm.addComponent(addButton);
		
		return addDomainForm;
	}
}
